package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.InvoiceDao;
import cn.itcast.dao.cargo.ShippingOrderDao;
import cn.itcast.domain.cargo.Invoice;
import cn.itcast.domain.cargo.ShippingOrder;
import cn.itcast.service.cargo.InvoiceService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @AuThor：zengxing
 * @DATE:2020/12/25 11:33  250
 */
@Service(timeout = 100000)
public class InvoiceServiceImpl implements InvoiceService {

    @Autowired
    private InvoiceDao invoiceDao;

    @Autowired
    private ShippingOrderDao shippingOrderDao;

    @Override
    public void save(Invoice invoice) {
        invoice.setStatus(0);
        invoice.setState(0);
        ShippingOrder shippingOrder = new ShippingOrder();
        shippingOrder.setShippingOrderId(invoice.getId());
        shippingOrder.setState(1);
        shippingOrderDao.updateByPrimaryKeySelective(shippingOrder);
        invoiceDao.save(invoice);
    }

    @Override
    public PageInfo findByPage(String companyId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Invoice> invoiceList = invoiceDao.findAll(companyId);
        return new PageInfo(invoiceList);
    }

    @Override
    public void delete(String id) {
        ShippingOrder shippingOrder = new ShippingOrder();
        shippingOrder.setShippingOrderId(id);
        shippingOrder.setState(0);
        shippingOrderDao.updateByPrimaryKeySelective(shippingOrder);
        invoiceDao.delete(id);
    }

    @Override
    public void update(Invoice invoice) {
        invoiceDao.update(invoice);
    }

    @Override
    public Invoice findById(String id) {
        return invoiceDao.findById(id);
    }
}
